package com.xonro.common.histrans.sfeg.job.custom;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.schedule.IJob;
import com.actionsoft.exception.AWSIllegalArgumentException;
import com.actionsoft.sdk.local.SDK;

import com.alipay.sofa.common.profile.StringUtil;
import com.xonro.common.histrans.base.consts.DrpConst;
import com.xonro.common.histrans.base.ctx.HistoryTransContext;
import com.xonro.common.histrans.sfeg.biz.custom.CompanyPastApplyBiz;
import com.xonro.common.histrans.sfeg.biz.custom.CompanyPastBoApplyBiz;
import com.xonro.common.histrans.zz.ctx.DrpProcessCreateContext;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Author: zhuhj
 * @Description: 客户信息登记流程（1.0）
 * @Date: 2023/12/28 10:02
 * @Version: 1.0
 */
public class CompanyPastApplyJob implements IJob {

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        //唯一标识
        String jobParam = SDK.getJobAPI().getJobParameter(jobExecutionContext);
        SDK.getLogAPI().consoleInfo("入参数为++++++"+jobParam);
        BO detail = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION").addQuery("SEQUENCE=", jobParam).detail();
        if (detail != null) {
            BO detailed = SDK.getBOAPI().query("BO_EU_HISTORICAL_DATA_MIGRATION_L").addQuery("BINDID=", detail.getBindId()).addQuery("IS_MAIN=", 1).detail();
            //drp主表名称
            String DrpName = detailed.getString("DRP_NAME");
           //BPM数据   删除旧数据
            String string = detailed.getString("BPM_NAME");
            List<BO> list1 = SDK.getBOAPI().query(string).list();
            if (list1!=null&&list1.size()>0){
                for (BO bo : list1) {
                    SDK.getProcessAPI().deleteById(bo.getBindId(),"admin");
                }
                SDK.getLogAPI().consoleInfo("删除完成++++++");
            }
            //备注
            String remark = detail.getString("REMARK");
            //数据来源
            String sourceDatabase = detail.getString("SOURCE_DATABASE");
            SDK.getLogAPI().consoleInfo("数据来源+++"+sourceDatabase);
            if (StringUtil.equals("1", sourceDatabase)) {
                SDK.getLogAPI().consoleInfo("进入1.0——————");
                //DRp流程
                List<RowMap> list = SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_DRP_1_CCID).getMaps("SELECT * FROM " + DrpName + " ");
               if (list!=null&&list.size()>0) {
                   SDK.getLogAPI().consoleInfo("DRp数据量++++"+list.size());
                   for (RowMap rowMap : list) {
                       //单据号
                       String stringCode = rowMap.getString("Code");
                       RowMap map = SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_DRP_1_WORKFLOW_CCID).getMap("SELECT * FROM WFI_FlowToken WHERE TokenName ='" + stringCode + "'");
                       //判断是流程还是BO
                       if (map != null && map.size() > 0) {
                           new CompanyPastApplyBiz(rowMap, detail, detailed).trans1();
                       } else {
                           new CompanyPastBoApplyBiz(rowMap, detail, detailed).trans1();
                       }
                   }
               }
            }
            else{
                SDK.getLogAPI().consoleInfo("进入2.0——————————————");
                List<RowMap> maps = SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMaps("SELECT * FROM " + DrpName + " ");
                SDK.getLogAPI().consoleInfo("数据条数+++"+maps.size());
                if (maps!=null&&maps.size()>0) {
                    for (RowMap map : maps) {
                        String wiid = map.getString("Wiid");
                        //BO
                        RowMap map1 = SDK.getCCAPI().getRDSAPI(DrpConst.ZZ_CCID).getMap("SELECT * FROM f2bpm_hi_processinstance WHERE   WorkflowInstanceId ='" + wiid + "' ");
                        //判断是流程还是BO
                        if (map1 != null && map1.size() > 0) {
                            new CompanyPastApplyBiz(map,detail,detailed).trans1();
                        }
                        else {
                            new CompanyPastBoApplyBiz(map,detail,detailed).trans1();
                        }
                    }
                }
            }
        }
        else {
            throw new AWSIllegalArgumentException("请先配置历史迁移映射配置表");
        }
    }
}
